"""
Copyright 2012 Wieser Dietmar, Margit Mutschlechner.

This file is part of Multimedia Social Networking Platform.

Multimedia Social Networking Platform is free software: 
you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Multimedia Social Networking Platform is distributed
in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Multimedia Social Networking Platform.  
If not, see <http://www.gnu.org/licenses/>.

"""

from PyQt4 import QtCore
from PyQt4 import QtGui
from msnp.gui import UIText, Resources, GUIHelper, MyStyle
from msnp import gui

class ProgressBarWidget(QtGui.QDialog):
    """This class contains the progress bar dialog which shows the progress of synchronization towards the media player.
    
    Parameters:
        __parent: A QWidget object containing the parent frame. Default None.
        __mainLayout: A QVBoxLayout object containing all object of this widget.
        __max: An integer value representing the maximum value of the progress bar.
        __topWidget: A QWidget object containing the items __lblImage and __lblProgress.
        __lblImage: A QLabel object.
        __lblProgress: A QLabel object.
        __progressbar: A QProgressBar object.
    """
    
    def __init__(self, max, isModal, parent=None):
        """The init-method."""
        QtGui.QWidget.__init__(self, parent)
        self.__max = max
        self.setWindowTitle(UIText.TITLE_PLEASE_WAIT)
        self.setWindowIcon(QtGui.QIcon(Resources.IMAGE_PROGRESS))
        self.setFixedSize(300, 100)
        self.__mainLayout = QtGui.QVBoxLayout()
        self.__build()
        self.setLayout(self.__mainLayout)
        self.setModal(isModal)
        GUIHelper.centerWidget(self)
        
    def __build(self):
        """Initializes the items contained in the widget."""
        self.__initProgressBar()
        self.__initLabels()
        self.__initTopWidget()
        self.__mainLayout.addWidget(self.__topWidget)
        self.__mainLayout.addWidget(self.__progressbar)
        #remove titlebar
        self.setWindowFlags(QtCore.Qt.CustomizeWindowHint)

    def __initTopWidget(self):
        """Initializes the QWidget object __topWidget and adds its items."""
        layout = QtGui.QHBoxLayout()
        self.__topWidget = QtGui.QWidget()
        layout.addWidget(self.__lblImage)
        layout.addWidget(self.__lblProgress)
        self.__topWidget.setLayout(layout)
        
    def __initProgressBar(self):
        """Initializes the QProgressBar item __progressbar."""
        self.__progressbar = QtGui.QProgressBar()
        self.__progressbar.setMinimum(0)
        self.__progressbar.setMaximum(self.__max)
        
    def __initLabels(self):
        """Initializes the QLabel objects __lblProgress and __lblImage."""
        self.__lblProgress = QtGui.QLabel("0/" + str(self.__max))
        self.__lblProgress.setAlignment(QtCore.Qt.AlignRight)
        self.__lblProgress.setFont(MyStyle.getFont8())
        self.__lblImage = QtGui.QLabel()
        self.__lblImage.setPixmap(QtGui.QPixmap(Resources.IMAGE_PROGRESS_SMALL))
        self.__lblImage.setAlignment(QtCore.Qt.AlignLeft)
        
    def updateProgressBar(self, progressCount):
        """Updates the status of the item __progressbar and sets it to the given value."""
        if progressCount < self.__max:
            self.__lblProgress.setText(str(progressCount) + "/" + str(self.__max))
            self.__progressbar.setValue(progressCount)
        else:
            self.close()
            
    def closeEvent(self, event):
        """This method is called when the widget is closing."""
        gui.GUIController.closeProgressBar()
        event.accept()
